package com.hr.classes.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.hr.classes.entity.po.TbAttendanceRulePO;
import com.hr.classes.entity.po.TbClassesPO;
import com.hr.classes.entity.vo.TbAttendanceRuleVO;
import com.hr.classes.entity.vo.TbClassesVO;
import com.hr.classes.service.TbAttendanceRuleService;
import com.hr.common.retrun.R;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * <pre>
 * 描述：TODO
 * </pre>
 *
 * @类名：com.hr.classes.controller.TbAttendanceRuleController
 * @创建日期: 2024/5/7 15:19
 */
@RestController
@Api(tags = "考勤规则管理")
@CrossOrigin
@RequestMapping("/tb/attendance/rule")
public class TbAttendanceRuleController {

    @Autowired
    private TbAttendanceRuleService tbAttendanceRuleService;

    @GetMapping("/list")
    public R getList(TbAttendanceRuleVO tbAttendanceRuleVO) {
        return R.ok(tbAttendanceRuleService.getList(tbAttendanceRuleVO));
    }

    @PostMapping("/add")
    public R add(@RequestBody TbAttendanceRuleVO tbAttendanceRuleVO) {
        return R.ok(tbAttendanceRuleService.add(tbAttendanceRuleVO));
    }

    @GetMapping("/get")
    public R get(@RequestParam Integer id) {
        return R.ok(tbAttendanceRuleService.get(id));
    }

    @PostMapping("/update")
    public R update(@RequestBody TbAttendanceRuleVO tbAttendanceRuleVO) {
        return R.ok(tbAttendanceRuleService.update(tbAttendanceRuleVO));
    }

    @PostMapping("/deletes")
    public R deletes(@RequestBody List<Integer> ids) {
        return R.ok(tbAttendanceRuleService.deletes(ids));
    }

    @GetMapping("/exportExcel")
    public void exportExcel(TbAttendanceRuleVO tbAttendanceRuleVO, HttpServletResponse response) throws IOException {
        String sheetName = "sheet名";
        String fileName = "导出数据.xlsx";
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");

        List<TbAttendanceRulePO> dataList = tbAttendanceRuleService.getListPO(tbAttendanceRuleVO);
        EasyExcel.write(response.getOutputStream(), TbAttendanceRulePO.class)
                .autoCloseStream(false)
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet(sheetName).doWrite(dataList);
    }

}
